package org.mengya.geekbang.sping.ioc.dependency.lookup;

import org.mengya.geekbang.sping.ioc.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @ClassName DependencyLookupDemo
 * @Description 依赖查找实例
 * 1. 通过名称的方式查找
 * @Author xiongwei.wu
 * @Date 2022/3/4 16:31
 **/
public class DependencyLookupDemo {

    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/dependency-lookup-context.xml");
        lookupByLazy(beanFactory);
        lookupByRealTime(beanFactory);
    }

    /**
     * 实时查找
     *
     * @param beanFactory
     */
    private static void lookupByRealTime(BeanFactory beanFactory) {
        User user = (User) beanFactory.getBean("user");
        System.out.println("实时查找：" + user);
    }

    /**
     * 延时查找
     *
     * @param beanFactory
     */
    public static void lookupByLazy(BeanFactory beanFactory) {
        ObjectFactory<User> objectFactory = (ObjectFactory<User>) beanFactory.getBean("objectFactory");
        User user = objectFactory.getObject();
        System.out.println("延迟" + user);
    }
}
